﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpLite.Domain.DataInterfaces;
using DAEProde.Domain;

namespace DAEProde.Tests.Fake
{
    public class FakeMatchDateRepository : IRepository<MatchDate>
    {
        public List<MatchDate> fechas;
        public FakeMatchRepository matchRepository;

        public FakeMatchDateRepository()
        {
            matchRepository = new FakeMatchRepository();

            fechas = new List<MatchDate> { 
                            null, //id = 0
                            new MatchDate { Name = "F1", Matches = new List<Match> { matchRepository.Get(1) } }, //id = 1
                            new MatchDate { Name = "F2", Matches = new List<Match> { matchRepository.Get(2) } }  //id = 2
                            };
        }

        public IDbContext DbContext
        {
            get { throw new NotImplementedException(); }
        }

        public void Delete(MatchDate entity)
        {
            fechas.Remove(entity);
        }

        public MatchDate Get(int id)
        {
            if (fechas.Count - 1 >= id) //el primer elemento null no se cuenta (id = 0)
            {
                return fechas.ElementAt(id);
            }
            else
            {
                return null;
            }
        }

        public IQueryable<MatchDate> GetAll()
        {
            return fechas.AsQueryable<MatchDate>();
        }

        public MatchDate SaveOrUpdate(MatchDate entity)
        {
            if (fechas.Exists(t => t == entity))
            {
                var toUpdate = fechas.Single(t => t == entity);
                return entity;
            }
            else
            {
                fechas.Add(entity);
                return entity;
            }
        }
    }
}


